package com.baolai.controller;

import com.alibaba.fastjson.JSONObject;
import com.baolai.entity.Shelf;
import com.baolai.result.Result;
import com.baolai.result.ResultUtil;
import com.baolai.service.ShelfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "货架")
@ResponseBody
@RestController
@RequestMapping("/shelf")
public class ShelfController {

    @Autowired
    ShelfService shelfService;

    @ApiOperation(value = "添加货架")
    @PostMapping("add")
    public Result addShelf(@RequestBody Shelf shelf){
        Shelf shelf1 = shelfService.getShelf(shelf.getId());
        if(shelf1 != null){
            return ResultUtil.error("该货架编号已存在");
        }
        if(shelfService.addShelf(shelf)){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @ApiOperation(value = "根据货架编号获取货架")
    @PostMapping("get")
    public Result getShelf(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(shelfService.getShelf(jsonObject.getInteger("id")));
    }

    @ApiOperation(value = "分页查询全部货架")
    @PostMapping("list")
    public Result listShelf(@RequestBody JSONObject jsonObject){
        return ResultUtil.success(shelfService.listShelf(jsonObject.getInteger("current"),jsonObject.getInteger("size")));
    }

    @ApiOperation(value = "更新货架信息")
    @PostMapping("update")
    public Result updateShelf(@RequestBody Shelf shelf){
        if(shelfService.updateShelf(shelf)){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.error("修改失败");
    }

}
